package com.yonyou.cyxdms.sysAuth.service.service.organization;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yonyou.cyx.function.utils.common.StringUtils;
import com.yonyou.cyxdms.sysAuth.service.entity.ComOrganizationPO;
import com.yonyou.cyxdms.sysAuth.service.mapper.ComOrganizationMapper;
import com.yonyou.cyxdms.sysAuth.service.vo.organization.OrganizationTreeVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * @author     ：quankk
 * @date       ：2019/7/11 14:41
 * @description：
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class OrganizationServiceImpl implements OrganizationService {

    // 定义日志接口
    private static final Logger logger = LoggerFactory.getLogger(OrganizationServiceImpl.class);

    @Autowired
    private ComOrganizationMapper comOrganizationMapper;

    @Override
    public List<OrganizationTreeVO> getIsValidOrgs() {
        List<ComOrganizationPO> list = comOrganizationMapper.selectList(new QueryWrapper<>());
        List<OrganizationTreeVO> orgList = new ArrayList<>();

        for(int i=0;i<list.size();i++){
            ComOrganizationPO orgMap = list.get(i);
            orgList.add(getOrginizationTree(orgMap));
        }
        return orgList;
    }

    private OrganizationTreeVO getOrginizationTree(ComOrganizationPO orgMap) {
        OrganizationTreeVO orgTreeOrg = new OrganizationTreeVO();
        String orgDeptId = orgMap.getOrgId().toString();
        String dealerCode = orgMap.getDealerCode();
        String orgCode = orgMap.getOrgCode();
        orgTreeOrg.setId(orgDeptId);
        String parent = "#";
        if(!StringUtils.isNullOrEmpty(orgMap.getParentOrgId())&&!dealerCode.equals(orgCode)){
            parent = orgMap.getParentOrgId().toString();
        }
        orgTreeOrg.setParent(parent);
        orgTreeOrg.setData(orgCode);
        orgTreeOrg.setText(orgMap.getOrgName().toString());
        return orgTreeOrg;
    }


    /**
     * @author     ：quankk
     * @date       ：2019/7/17 14:52
     * @description：gms端查询有效组织
     */
    @Override
    public List<OrganizationTreeVO> getIsValidOrgsGms(String dataType) {
        List<Map> list = comOrganizationMapper.getIsValidOrganizationGMS(dataType);
        List<OrganizationTreeVO> orgList = new ArrayList<>();
        for(int i=0;i<list.size();i++){
            Map orgMap = list.get(i);
            orgList.add(getOrginizationTree(orgMap));
        }
        return orgList;
    }

    /**
     *
     * 组织经销商树的结构
     * @author zhangxc
     * @date 2017年3月21日
     * @param orgMap
     * @return
     */
    private OrganizationTreeVO getOrginizationTree(Map orgMap){
        OrganizationTreeVO orgTreeOrg = new OrganizationTreeVO();
        String orgDeptId = orgMap.get("ORGDEPT_ID").toString();
        orgTreeOrg.setId(orgDeptId);
        String parentOrgId =null;
        if(orgMap.get("PARENT_ORG_ID")!=null){
            parentOrgId= orgMap.get("PARENT_ORG_ID").toString();
        }

        String parent = "#";
        if(!StringUtils.isNullOrEmpty(parentOrgId)&&!orgDeptId.equals(parentOrgId)){
            parent = orgMap.get("PARENT_ORG_ID").toString();
        }
        orgTreeOrg.setParent(parent);
        if(!StringUtils.isNullOrEmpty(orgMap.get("ORG_CODE"))){
            orgTreeOrg.setData(orgMap.get("ORG_CODE").toString());
        }
        orgTreeOrg.setText(orgMap.get("ORG_NAME").toString());
        return orgTreeOrg;
    }

}
